Skip to content

Wasm: dispatch lifecycle events when async instantiation completes#2232

Open
JasonGross wants to merge 5 commits into
ocsigen:masterfrom
JasonGross:wasm-lifecycle-events
Open

Wasm: dispatch lifecycle events when async instantiation completes#2232
JasonGross wants to merge 5 commits into
ocsigen:masterfrom
JasonGross:wasm-lifecycle-events

Conversation

@JasonGross

@JasonGross JasonGross commented May 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Test plan

  • make lint-js passes (only an unrelated biome.json schema-version notice)
  • dune build runtime/wasm succeeds
  • dune build compiler/lib compiler/lib-wasm compiler/bin-wasm_of_ocaml compiler/bin-js_of_ocaml runtime succeeds (verifies the free-variable check accepts CustomEvent)
  • make tests — could not run locally; this switch lacks ppx_expect and other test-only dependencies. CI should cover it.
  • In a browser, a wasmoocaml:loaded event fires on globalThis after the Wasm runtime finishes initializing
  • When instantiation throws, a wasmoocaml:error event fires before the error propagates

🤖 Generated with Claude Code

@JasonGross

Copy link
Copy Markdown
Contributor Author

I assume all of the CI failures are spurious?

@hhugo
hhugo force-pushed the wasm-lifecycle-events branch from b0ea27b to e5fc28c Compare May 6, 2026 13:01
@hhugo

hhugo commented Jul 11, 2026

Copy link
Copy Markdown
Member

re-looking at this, I wonder if we should have a symmetric behavior in the js_of_ocaml side so that it's easier to switch between backend

@JasonGross
JasonGross force-pushed the wasm-lifecycle-events branch from 2b9ee02 to cedc3a4 Compare July 11, 2026 17:05
@JasonGross

Copy link
Copy Markdown
Contributor Author

re-looking at this, I wonder if we should have a symmetric behavior in the js_of_ocaml side so that it's easier to switch between backend

Done

JasonGross and others added 5 commits July 15, 2026 21:11
Compiling and instantiating the generated WebAssembly is asynchronous,
but the launcher only returns a Promise that callers typically don't
await. Surrounding JavaScript that runs on DOMContentLoaded therefore
has no way to tell when the OCaml exports are actually ready. Dispatch
'wasmoocaml:loaded' (and 'wasmoocaml:error' on failure) CustomEvents
on globalThis so external code can wait for the runtime to be ready.

Ported from ocaml-wasm/wasm_of_ocaml#143.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The free-variable check in compiler/bin-wasm_of_ocaml/gen/gen.ml flagged
the new CustomEvent reference in runtime.js. CustomEvent is a standard
DOM/Node global comparable to XMLHttpRequest and DOMException already in
this list.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Mirror the Wasm launcher: JavaScript executables now dispatch
jsoocaml:loaded / jsoocaml:error CustomEvents on globalThis when their
toplevel initialization completes or fails, so surrounding JavaScript
can wait for the program the same way with either backend. Separate
compilation units, --wrap-with-fun output, build-runtime and build-fs
scripts are unaffected.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KW1ybsago5w9iAgbjah2kf
The exports expect-test filters top-level statements for jsoo_exports
usage; standalone executables are now wrapped in a try/catch dispatching
lifecycle events, so teach the filter to look for the program inside
that wrapper.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KW1ybsago5w9iAgbjah2kf
Cosmetic restructuring of the lifecycle-event application in pack, to
keep the Iife branch closer to the formatting conventions used in this
file.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KW1ybsago5w9iAgbjah2kf
@JasonGross
JasonGross force-pushed the wasm-lifecycle-events branch from 8ea50ee to 1b6238a Compare July 15, 2026 21:19
@JasonGross

Copy link
Copy Markdown
Contributor Author

CI failure seems like a flake:
[ERROR] Failed to get sources of xtmpl.1.2.0: curl failed

#=== ERROR while fetching sources for xtmpl.1.2.0 =============================#
OpamSolution.Fetch_fail("https://www.good-eris.net/xtmpl/releases/xtmpl-1.2.0.tar.bz2 (curl failed: "/usr/bin/curl --write-out %{http_code}\\n --retry 10 --retry-delay 2 --user-agent opam/2.5.2 -L -o /tmp/opam-53840-0f5ff8/xtmpl-1.2.0.tar.bz2.part -- [https://www.good-eris.net/xtmpl/releases/xtmpl-1.2.0.tar.bz2](https://www.good-eris.net/xtmpl/releases/xtmpl-1.2.0.tar.bz2/)" exited with code 28)")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] wasm is not ready when DOMContentLoaded fires and does not trigger an event when it is loaded

4 participants